// Filename:    AutoJob_Client.cpp
// Author:      John Van Drasek
// Date:		3/11/2011
// Description:


// Includes
// Windows
#include <stdio.h>
// RUP
#include "RUP.h"
// Threading
#include "pthread.h"
// LUA
extern"C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
// AutoJob_Client
#include "Chat.h"
#include "AutoJob_Client_Globals.h"


static const int MAXSIZE = 256;

//--------------------------------------------------------------------------------------
// Start the chat receiver
//--------------------------------------------------------------------------------------
void SetupChatReceiver()
{
	// Get the Listbox from the window manager
	int tRetVal;

	// Start thread to monitor incoming text chat
	int ptr = 0;
	tRetVal = pthread_create(&tReceiveChat,NULL,ReceiveChatFromServer,(void*)ptr);
	tRetVal = pthread_detach(tReceiveChat);
}


//--------------------------------------------------------------------------------------
// Stop the chat receiver
//--------------------------------------------------------------------------------------
void StopReceiveChatFromServer()
{
	//printf("Stopping chat server...\n");  // TODO: write to log file
	if(!(pthread_cancel(tReceiveChat))){
		printf("Chat receiver was stopped successfully!\n");  // TODO: write to log file
	}
	else
	{
		// TODO: write to log file
		//printf("Something went wrong while trying to stop chat receiver!  :(\n");
	}
}


//--------------------------------------------------------------------------------------
// Print message
//--------------------------------------------------------------------------------------
void PrintMessage()
{
	printf("\n\nWaiting for incoming data from AutoJob server...\n");
	printf("Type \"exit\" and press enter to close this window\n\n");
}

//// Declare any and all libs used in this code as well as the script file
//static const luaL_reg LuaLibs[] =
//{
//	{ "base", luaopen_base },
//	{ NULL, NULL }
//};
//
//// Open all the libs declared above
//static void OpenLibs(lua_State* lua_state)
//{
//	const luaL_reg* lua_lib;
//
//	for (lua_lib = LuaLibs; lua_lib->func != NULL; lua_lib++)
//	{
//		lua_lib->func(lua_state);
//		lua_settop(lua_state, 0);
//	}
//}

//--------------------------------------------------------------------------------------
// Main entry point
//--------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
	//char* scriptName = NULL;

	//if(argc != 2)
	//{
	//	printf("Please provide a script to execute as the input to the program.\n");
	//	return 0;
	//}
	//else
	//{
	//	int size = strnlen(argv[1], MAXSIZE);
	//	scriptName = (char*)malloc(size+1);
	//	memset(scriptName, 0, size+1);
	//	memcpy(scriptName, argv[1], size+1);
	//}

	//// Declare the lua state
	//lua_State *lua_state;
	//// Open the lua state
	//lua_state = lua_open();
	//// Load the libs
	//OpenLibs(lua_state);

	//// Run the lua script
	//luaL_dofile(lua_state, scriptName);

	//// Destroy the lua state
	//lua_close(lua_state);

	char str[COMMANDSIZE];
	memset(str,0,COMMANDSIZE);

	// Start chat receiver
	SetupChatReceiver();

	printf("\n******************\n");
	printf("* AutoJob Client *\n");
	printf("******************\n\n");

	while((strcmp(((char*)(&str)),"exit") != 0))
	{
		PrintMessage();
		memset(str,0,COMMANDSIZE);
		scanf("%s",str);  // FIXME: this needs to be changed, but scanf_s causes problems :/ need to look into it
	}

	// Shutdown chat receiver
	StopReceiveChatFromServer();
	return 0;
}